<?php


namespace app\api\model;

use think\Exception;
use think\Model;
use TencentCloud\Common\CommonClient;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;

/**
 * 语音识别
 * Class VoiceModel
 * @package app\api\model
 *
 */
class VoiceModel extends Model
{



    /**
     *
     * 语音识别
     */
    public static function recordDistinguish($url){

        try {
            $data = [

                'Url'=>$url,
                'ChannelNum'=>1,
                'EngineModelType'=>'8k_zh',
                'ResTextFormat'=>5,
                'SourceType'=>0,
                'SpeakerDiarization'=>1,
                'SpeakerNumber'=>0
            ];
            $cred = new Credential("AKIDUt5UmyLtQp595qTJAtfJyUntwqY8slT2", "SglhVCfuvi1FNK3b5U6QjAX2SiuBOWXW");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("asr.tencentcloudapi.com");
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new CommonClient("asr", "2019-06-14", $cred, "", $clientProfile);
            $params = json_encode($data);
            $resp = $client->callJson("CreateRecTask", json_decode($params));
//            halt($resp);
            return $resp['Data']['TaskId'];
        }
        catch(TencentCloudSDKException $e) {
            throw new Exception($e->getMessage());
        }
    }

    /**
     *
     * 语音识别结果
     */
    public static function recordDistinguishResult($TaskId){

        try {
            $cred = new Credential("AKIDUt5UmyLtQp595qTJAtfJyUntwqY8slT2", "SglhVCfuvi1FNK3b5U6QjAX2SiuBOWXW");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("asr.tencentcloudapi.com");
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new CommonClient("asr", "2019-06-14", $cred, "", $clientProfile);
//            halt($resp);

            $data = [

                'TaskId'=>(int)$TaskId
            ];

            $params = json_encode($data);
            $resp = $client->callJson("DescribeTaskStatus", json_decode($params));
            return $resp;
        }
        catch(TencentCloudSDKException $e) {
            throw new Exception($e->getMessage());
        }
    }

}